The present invention relates to computer graphics processing, and more particularly to stencil testing.
A major objective in graphics rendering is to produce images that are so realistic that the observer believes the image is real. A fundamental difficulty in achieving total visual realism is the complexity of accurately representing real world visual effects. A scene can include a wide variety of textures, subtle color gradations, reflections, translucency, etc.
One important way to make images more realistic is to determine how objects in a scene cast shadows and then represent these shadows in the rendered image. Shadows enhance the realism of an image because they give a two-dimensional image a three-dimensional feel.
The addition of shadows to the rendering of a complex model can greatly enhance the understanding of that model""s geometry. The human visual system uses shadows as a cue for depth and shape. Consequently, shadows are very useful for conveying the three dimensional nature of rendered objects and for adding realism to computer generated scenes.
Prior art FIG. 1 illustrates a scene 100 where shadowing is needed during processing in a graphics pipeline. As shown in FIG. 1, a particular scene 100 includes a light source 104, a light obstructing surface 102, and a plurality of objects 106. Due to the relative orientation of the light source 104, objects 106 and surface 102, a shadow should be shown on certain objects 106 where the surface 102 obstructs light from the light source 104.
The utilization of shadow volumes is a technique by which a graphics pipeline may determine which objects 106 in the scene 100 should be rendered with a shadow. In particular, a shadow bit is maintained for each pixel in a frame buffer which indicates whether the objects 106 are in or out of the shadow projected by the surface 102.
Since the shadow volume technique takes place in a frame buffer of a graphics pipeline, it may be referred to as an image space algorithm. While shadowing may be trivial in a scene with one obstructing surface 102, rendering may be complicated when multiple overlapping obstructing surfaces 102 exist.
Prior art FIG. 2 illustrates a scene 200 where shadowing is complicated by the involvement of multiple intersecting overlapping surfaces 102. In such more complex environments, a technique of xe2x80x9ccountingxe2x80x9d is employed. As shown in Prior art FIG. 2, a plurality of obstructing surfaces 102 cast a plurality of shadow volumes each defined by a plurality of boundary primitives 202.
Such boundary primitives 202 are either front-facing or back-facing with respect to how they are viewed. In the context of the present description, a primitive is front-facing or back-facing based on whether the area of the primitive in screen space is positive or negative, or the ordering of vertices of the primitive is clockwise or counterclockwise. Note Prior art FIG. 2 where front-facing and back-facing primitives are designated with an xe2x80x9cFxe2x80x9d and xe2x80x9cB,xe2x80x9d respectively.
Prior art FIG. 3 illustrates a method 300 for counting shadow volumes, in accordance with the prior art. Initially, in operation 302, a depth buffer and a stencil buffer are cleared after which the scene 200 is rendered with the light source 102 disabled during a first rendering pass. Once this pass is rendered, the color of all pixels in the frame buffer is as if every pixel were in shadow with respect to the light. Note operation 304. It should, be noted that such operation affords a depth state of a closest object in the scene 200.
Next, in operation 306, primitives constituting the geometry of the scene""s shadow volumes are drawn in the scene 200 during a second rendering pass. During the second pass, no color depth buffer writes take place in order to prevent disturbing results of the first rendering pass. Further, any back-facing shadow volume primitives are culled. For the remaining front-facing primitives, a per-pixel stencil value is incremented for each boundary between an eye position 206 and a subject object 204. This is accomplished by incrementing the per-pixel stencil value whenever the depth test passes. Since there are three front-facing primitive boundaries, such stencil value has the value of +3 after operation 306.
Next, in operation 308, shadow volume primitives are again drawn in the scene during a third rendering pass. Similar to the second pass, no color depth buffer writes take place in order to prevent disturbing results of the previous rendering passes. In operation 308, any front-facing primitives are culled. For the remaining back-facing primitives, a per-pixel stencil value is decremented for each boundary between the eye position 206 and the subject object 204. This is accomplished by decrementing the per-pixel stencil value whenever the depth test passes. Since there are three back-facing primitive boundaries, such stencil value has the value of xe2x80x9c0xe2x80x9d after operation 306.
The scene may then be rendered again during a fourth rendering pass in operation 310. During the fourth rendering pass, the light source 102 is enabled. Color writes are re-enabled for this rendering pass. Moreover, pixels are only updated if it is determined that the pixel is outside a shadow volume, as indicated by the stencil value being equal to xe2x80x9c0.xe2x80x9d In the case of object 204 of Prior art FIG. 2, such associated pixels would be updated to show light. However, in the case of object 206, no light would be shown since the stencil value would be +2 so the stencil test fails which leaves the pixel unchanged from the initial shadowed rendering pass, etc.
One problem that results from the method 300 of Prior art FIG. 3 stems from the fact that the shadow volumes must be rendered twice during operations 306 and 308. The requirement that these rendering passes be separate operations 306 and 308 is prompted by the fact that traditional stencil value testing can not distinguish between front-facing and back-facing primitives. As such, the front-facing and back-facing primitives must be handled separately, in different rendering passes. This results in an efficiency problem that causes lower performance during graphics processing.
A system, method and computer program product are provided for two-sided stencil testing during graphics processing. Initially, primitives are received to be processed in a graphics processing pipeline. In use, it is then determined whether the graphics processing pipeline is operating with same-sided stencil testing enabled.
If same-sided stencil testing is not enabled, the primitives are passed without same-sided stencil testing and without two-sided stencil testing. If, on other hand, same-sided stencil testing is enabled, it is determined whether the graphics processing pipeline is operating with two-sided stencil testing enabled.
If the two-sided stencil testing is enabled and the same-sided stencil testing is enabled, two-sided stencil testing is performed on the primitives. If, on the other hand, the two-sided stencil testing is disabled and the same-sided stencil testing is enabled, same-sided stencil testing is performed on the primitives.
In one embodiment, the two-sided stencil testing may include determining whether the primitives are front-facing primitives or back-facing primitives. Further, stencil testing may be performed on the front-facing primitives utilizing a front set of stencil states. In a similar manner, the stencil testing may be performed on the back-facing primitives utilizing a back set of stencil states.
As an option, the front-facing primitives and back-facing primitives may include front-facing polygon primitives and back-facing polygon primitives, respectively. The front-facing primitives may also include front-facing non-polygon primitives. Moreover, the front-facing non-polygon primitives may include points, lines, bitmaps, and:image rectangles.
In one aspect of the present embodiment, the stencil states may include stencil operations, reference values, compare masks, compare functions, and write masks.
In use, the two-sided stencil testing is performed in a single pass, thus contributing to the efficiency of the overall system. While the present technique has many applications, the two-sided stencil testing may be performed to facilitate a shadowing operation involving shadow volumes, in accordance with one embodiment.
One system, method and computer program product is provided for use of two-sided stencil testing during graphics processing including shadow volumes. Upon primitives representative of shadow volumes being received to be processed in a graphics processing pipeline, such primitives are rendered in a single rendering pass.
Such a single rendering pass includes determining whether the primitives are front-facing or back-facing. If the primitives are front-facing, a depth state is incremented. Moreover, if the primitives are back-facing, the depth state is decremented. A pixel may then conditionally be rendered with a shadow thereon based on a value of the depth state.
These and other advantages of the present invention will become apparent upon reading the following detailed description and studying the various figures of the drawings.